Skip to content

Add polyfills for C# 15 union types (UnionAttribute and IUnion) - #140

Merged
Sergio0694 merged 5 commits into
Sergio0694:mainfrom
AndiJingzhiTse:dev/union-types-polyfill
Oct 26, 2026
Merged

Sergio0694 merged 5 commits into
Sergio0694:mainfrom
AndiJingzhiTse:dev/union-types-polyfill

Conversation

@AndiJingzhiTse

Copy link
Copy Markdown
Contributor

Summary

Closes #139

This PR adds source-only polyfills for the two types required by the C# 15 union types feature, enabling their use on downlevel targets before .NET 11 ships these types in the BCL:

  • [Union] (System.Runtime.CompilerServices.UnionAttribute) — attribute recognized by the C# 15 compiler to identify a type as a union type
  • IUnion (System.Runtime.CompilerServices.IUnion) — interface that union types implement, providing access to the underlying value via object? Value { get; }

Both are placed in the main EmbeddedResources/ folder (language support polyfills, enabled by default) and follow the existing conventions: // <auto-generated/> header, internal accessibility (switchable to public via PolySharpUsePublicAccessibilityForGeneratedTypes), and [ExcludeFromCodeCoverage] on the attribute class.

References

  • C# 15 union types blog post
  • The blog post notes: "in .NET 11 Preview 2, the UnionAttribute and IUnion interface aren't included in the runtime yet. You must declare them in your project."

Test plan

  • Added TestUnion class in tests/PolySharp.Tests/LanguageFeatures.cs that applies [Union] and implements IUnion, exercising both new polyfills
  • Build passes: dotnet build — 0 warnings, 0 errors
  • All tests pass across all target frameworks (net472, net48, net481, netstandard2.0, netstandard2.1, net8.0, net9.0)

🤖 Generated with Claude Code

@AndiJingzhiTse
AndiJingzhiTse marked this pull request as draft May 2, 2026 16:56
@AndiJingzhiTse
AndiJingzhiTse marked this pull request as ready for review May 2, 2026 17:17
@AndiJingzhiTse
AndiJingzhiTse force-pushed the dev/union-types-polyfill branch from 73fd312 to d27001b Compare May 2, 2026 20:15
Comment thread README.md Outdated
@universorum

Copy link
Copy Markdown

.NET 11 RC is out and it already has go-live support for using in production. When can we see this PR merged?

Comment thread tests/PolySharp.Tests/LanguageFeatures.cs Outdated
@michael-hawker

Copy link
Copy Markdown

@Sergio0694 mind kicking off the workflow? Assuming it drops an accessible artifact, I can try the private build in my project for final testing.

@Sergio0694

Copy link
Copy Markdown
Owner

Approved! I'll try to get to this PR later this week 🙂

@michael-hawker michael-hawker left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pulled the NuGet package from the pipeline to test locally in my .NET 8 Godot project swapping out my manual proxies. I see the generator adding them in and it works like a charm! 🚀

@xanth

xanth commented Oct 23, 2026

Copy link
Copy Markdown

Any update on this?

AndiJingzhiTse and others added 4 commits October 25, 2026 18:22
Adds source-only polyfills for the UnionAttribute and IUnion interface
from System.Runtime.CompilerServices, enabling C# 15 union type features
on downlevel targets before .NET 11 ships these types in the BCL.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…e.CompilerServices.UnionAttribute.cs

Co-authored-by: Theodore Tsirpanis <teo@tsirpanis.gr>
Co-authored-by: Theodore Tsirpanis <teo@tsirpanis.gr>
@Sergio0694
Sergio0694 force-pushed the dev/union-types-polyfill branch from 0d4c044 to 8fd413e Compare October 26, 2026 01:26
Synchronize README entries, align API documentation, and cover generated and manual unions with non-boxing accessors.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

@Sergio0694 Sergio0694 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! 🙂

@Sergio0694
Sergio0694 merged commit f4c55c7 into Sergio0694:main Oct 26, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Polyfill for union types

6 participants